package com.walterweimod.auxiliary_attributes.event.new_event;

import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.EntityDamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.*;

//@Mod.EventBusSubscriber
public class TestEvent {
    @SubscribeEvent
    public static void test(LivingAbsorptionChangeEvent event){
        getLogger().info("已经挂上钩子!!!");

        LivingEntity livingEntity = event.getEntityLiving();

        float add_amount = event.getAmount() - livingEntity.getAbsorptionAmount();

        if (add_amount > 0.0F){
            add_amount *= 1.35F;

            event.setAmount(livingEntity.getAbsorptionAmount() + add_amount);

            getLogger().info("玩家护盾值：{}", event.getAmount());
            getLogger().info("玩家获得护盾量提升35%!!!");
        }
    }

    @SubscribeEvent(priority = EventPriority.LOW)
    public static void test2(LivingHurtEvent event){
        LivingEntity livingEntity = event.getEntityLiving();

        if (livingEntity.getAbsorptionAmount() > 0.0F){
            event.setAmount(event.getAmount() * 0.75F);

            getLogger().info("持有护盾的玩家受到伤害降低25%!!!");
        }
    }

//    @SubscribeEvent
    public static void test3(LivingHurtEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        DamageSource damageSource = event.getSource();
        Entity sourceEntity = damageSource.getEntity();

        if (sourceEntity instanceof Player player
                && player.getAbsorptionAmount() > 0.0F){
            int last_hurt_time = livingEntity.hurtTime;

            livingEntity.hurtTime = 0;

            EntityDamageSource test = new EntityDamageSource("test", sourceEntity);

            if (livingEntity instanceof EnderDragon enderDragon){
                enderDragon.hurt(enderDragon.head, test, player.getAbsorptionAmount() * 1.6F);
            }else {
                livingEntity.hurt(test, player.getAbsorptionAmount() * 1.6F);
            }

            livingEntity.hurtTime = last_hurt_time;

            getLogger().info("玩家攻击，实体受到反震伤害!!!");
        }
    }

    @SubscribeEvent
    public static void test4(LivingDamageEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        DamageSource damageSource = event.getSource();

        if (livingEntity instanceof Player player
                && player.getAbsorptionAmount() > 0.0F
                && damageSource.getEntity() instanceof LivingEntity sourceEntity){
            int last_hurt_time = sourceEntity.hurtTime;

            sourceEntity.hurtTime = 0;

            EntityDamageSource test = new EntityDamageSource("test", livingEntity);

            if (sourceEntity instanceof EnderDragon enderDragon){
                enderDragon.hurt(enderDragon.head, test, player.getAbsorptionAmount() * 1.6F);
            }else {
                sourceEntity.hurt(test, player.getAbsorptionAmount() * 1.6F);
            }

            sourceEntity.hurtTime = last_hurt_time;

            getLogger().info("玩家受到攻击，实体受到反震伤害!!!");
        }
    }
}
